# -*- encoding: utf-8 -*-
"""
@File    : 三乐购物系统.py
@Time    : 2020/10/28 9:33
@Author  : lluyao
@Email   : lluyao61@163.com
@Software: PyCharm
"""
import json,sys

def user_choice():
    print('==========用户登录===========')
    print('==1.登录')
    print('==2.注册')
    print('=============================')

def operate():
    print('==========用户操作===========')
    print('==1.购物')
    print('==2.充值')
    print('==3.查看余额')
    print('==4.查看购物清单')
    print('=============================')

def menu():
    global  goods_menu
    goods_menu={'芒果':7,
                '火龙果':5,
                '香蕉':3,
                '苹果':5,
                '梨子':8,
                '桃子':5
                }
    print('==========商品信息===========')
    i=1
    for name,price in goods_menu.items():
        print(f'{i}.{name}——{price}元/斤')
        i+=1
    print('=============================')

def register():
    print('============注册=============')
    name = input('请输入用户名：')
    user_exit(name)
    with open('user.txt','r',encoding='utf-8') as f:
        s = f.read()
        #json.loads  是把json str---》 dict
        user_dict = json.loads(s)
    if name in user_dict:
        print('用户名已存在')
    else:
        pwd = input('请输入密码：')
        user_exit(pwd)
        with open('user.txt', 'w', encoding='utf-8') as f:
            user_dict[name] = pwd
            #把dict --》 json str
            user_str=json.dumps(user_dict)
            f.write(user_str)
        with open('user_count.txt', 'w+', encoding='utf-8') as f:
            s = f.read()
            user_count_dict = json.loads(s)
        with open('user_count.txt','w',encoding='utf-8') as f:
            user_count_dict[name] = 0
            f.write(json.dumps(user_count_dict))
            print('注册成功!')

def login():
    print('============登录=============')
    with open('user.txt','r',encoding='utf-8') as f:
        user_str = f.read()
        user_dict = json.loads(user_str)
        while True:
            name = input('请输入用户名：')
            user_exit(name)
            if name in set(user_dict) :
                pwd = input('请输入密码：')
                user_exit(pwd)
                if user_dict[name] == pwd:
                    print('登陆成功！')
                    return name
                else:
                    print('密码错误！')
            else:
                print('用户名未注册！')

def user_shop(user_name):
    while True:
        name = input('请输入您要买的商品名称(按q结束购买)：')
        user_exit(name)
        if name == 'q':
            break
        if name not in goods_menu:
            print('输入不合法，请重新输入！')
            continue
        num = input('请输入需要购买的斤数：')
        user_exit(num)
        if num.isdigit():
            with open('user_count.txt', 'r', encoding='utf-8') as f:
                s = f.read()
                user_count_dict = json.loads(s)
                if user_count_dict[user_name] - int(num) * goods_menu[name] < 0:
                    print('余额不足！请充值！')
                else:
                    if name not in goods_list:
                        goods_list[name] = 0
                    goods_list[name] += int(num)
                    with open('user_count.txt', 'w', encoding='utf-8') as f:
                        user_count_dict[user_name] -= int(num) * goods_menu[name]
                        f.write(json.dumps(user_count_dict))
                        print('购买成功！')
        else:
            print('输入不合法！')

def recharge(name):
    money = input('请输入充值金额：')
    user_exit(money)
    with open('user_count.txt','r',encoding='utf-8') as f:
        s = f.read()
        user_count_dict = json.loads(s)
        user_count_dict[name] += int(money)
        print(f'当前余额为:￥{user_count_dict[name]}')

    with open('user_count.txt', 'w', encoding='utf-8') as f:
        f.write(json.dumps(user_count_dict))

def balance(name):
    with open('user_count.txt', 'r', encoding='utf-8') as f:
        s = f.read()
        user_count_dict = json.loads(s)
        print(f'当前余额为:￥{user_count_dict[name]}')

def shop_list():
    if len(goods_list) == 0:
        print('————您没有购买任何东西！————')
        # return 0
    else:
        print('———————购物清单———————')
        print('序号    商品    单价    斤     总价')
        i=1
        all_counts = 0
        for name, num in goods_list.items():
            counts = int(goods_menu[name])*int(num)
            all_counts += counts
            print(f'{i}       {name}    {goods_menu[name]}/斤    {num}斤    {counts}元')
            i += 1
        print(f'合计                           {all_counts}元')
        print('——————————————————')
        # return all_counts

def user_exit(user_input):
    if user_input == 'exit':
        shop_list()
        print('退出！')
        sys.exit()

goods_menu = {}#商品菜单
goods_list = {}#用户购物清单
while True:
    user_choice()
    choice = input('请输入(任意位置输入exit退出)：')
    user_exit(choice)
    if choice == '1':
        username = login()
        if username != '':
            while True:
                operate()
                choice = input('请输入：')
                user_exit(choice)
                if choice == '1':
                    menu()
                    user_shop(username)
                elif choice == '2':
                    recharge(username)
                elif choice == '3':
                    balance(username)
                elif choice == '4':
                    shop_list()
                elif choice == 'exit':
                    user_exit(choice)
                else:
                    print('您的输入有误，请重新输入')
    elif choice == '2':
        register()
    else:
        print('您的输入有误，请重新输入')
